import {
    config
} from "../config.js";
const tips = {
    1: "抱歉出现了错误",
    1005: "APPkey没有",
    3000: "期刊不错"
}
class HTTP {
    request({url, data = {}, method = "GET"}) {   //可以用对象{}包裹 ，ES6解构
        return new Promise((reslove, reject) => {
            this._request(url, reslove, reject, data ,method )
        })
    }

    _request(url, reslove, reject, data = {}, method = "GET") {
        wx.request({
            url: config.api_base_url + url,
            method: method,
            data: data,
            header: {
                "content-type": "application/json",
                "appkey": config.appkey
            },
            success: (res) => {
                const code = res.statusCode.toString();
                if (code.startsWith("2")) //字符串开始的第一个字母包含2
                {
                    reslove(res.data)

                } else {
                    reject()
                    const error_code = res.data.error_code
                    this._show_error(error_code)
                }
            },
            fail: (err) => {
                reject()
                this._show_error(1)
            }
        })
    }
    _show_error(error_code) {
        if (!error_code) {
            error_code = 1
        }
        const tip =tips[error_code]
        wx.showToast({
            title: tip?tip:tips[1],
            icon: "none",
            duration: 2000
        })
    }
}
export {
    HTTP
}